package com.travel.order.controller;

import com.travel.common.base.BaseApiService;
import com.travel.common.base.Resp;
import com.travel.common.utils.OrderNoUtils;
import com.travel.document.service.DocumentService;
import com.travel.goods.entity.Goods;
import com.travel.goods.service.GoodsService;
import com.travel.order.entity.OrderEntity;
import com.travel.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @classDesc: 功能描述:(订单控制)
 * @author:<a href="leeue@foxmail.com">李月</a>
 * @Version:v1.0
 * @createTime:2019年3月19日 下午2:40:04
 */
@RestController
@RequestMapping("/order")
@Slf4j
@Api(tags = "订单模块")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    BaseApiService baseApiService;

    @Autowired
    DocumentService documentService;

    @Autowired
    GoodsService goodsService;

    /**
     * 下单
     */
    @ApiOperation(value = "下单接口")
    @ApiImplicitParam(name = "orderEntity", value = "订单实体类", dataType = "OrderEntity", required = true)
    @PostMapping("/add")
    public Resp saveOrder(@RequestBody OrderEntity orderEntity) {

        if (orderEntity.getUserId() == null) {
            return baseApiService.setResultError("用户当前未登录，无法下单");
        }
        if (orderEntity.getGoodsId() == null) {
            return baseApiService.setResultError("选取商品为空，无法下单");
        }
        if (orderEntity.getOrderCount() == null || orderEntity.getOrderCount() == 0) {
            return baseApiService.setResultError("订单数量不能为0");
        }

        Optional<Goods> opGoods = goodsService.findByGoodsId(orderEntity.getGoodsId());

        if (opGoods.get() == null) {
            return baseApiService.setResultError("商品不能为空");
        }

        Goods goods = opGoods.get();

        orderEntity.setOrderMoney(goods.getGoodsMoney());

        log.info("####保存订单#####");
        // 计算总金额
        Double totalMoney = orderEntity.getOrderMoney() * orderEntity.getOrderCount();
        orderEntity.setOrderTotalMoney(totalMoney);

        // 查找当前商品的封面
        List<String> imgPath = documentService.findByObjId(orderEntity.getGoodsId());

        // 订单封面
        if (imgPath != null && imgPath.size() > 0) {
            orderEntity.setOrderImgPath(imgPath.get(0));
        }
        // 生成唯一订单号
        Long userId = orderEntity.getUserId();
        orderEntity.setOrderNo(OrderNoUtils.getOrderNo(userId));

        OrderEntity order = orderService.saveOrder(orderEntity);

        return baseApiService.setResultSuccess("保存成功", order);
    }

    /**
     * 根据用户ID来查找订单
     */
    @ApiOperation(value = "根据用户ID来查找订单")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", required = true)
    @PostMapping("/findOrderByUserId")
    public Resp findOrderByUserId(Long userId) {
        if (userId == null || userId == -1) {
            return baseApiService.setResultError("用户当前未登录,无法查询订单");
        }

        // 查询当前用户的所有订单 一个订单对应一个商品
        List<OrderEntity> orders = orderService.findByUserId(userId);

        return baseApiService.setResultSuccess(orders);
    }

    /**
     * 根据用户ID和订单ID来查询状态
     */

    @ApiOperation(value = "根据用户ID和订单ID来查询订单状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", dataType = "String", required = true)})
    @PostMapping("/findByUserIdAndOrderStatus")
    public Resp findByUserIdAndOrderStatus(Long userId, String orderStatus) {
        if (userId == null) {
            return baseApiService.setResultError("用户当前未登录,无法查询订单");
        }

        List<OrderEntity> orders = orderService.findByUserIdAndOrderStatus(userId, orderStatus);

        return baseApiService.setResultSuccess(orders);
    }

    /**
     * 根据用户ID查询该用户所有的订单
     */
    @ApiOperation(value = "根据用户ID查询该用户所有的订单")
    @ApiImplicitParams(@ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", required = true))
    @PostMapping("/findByUserId")
    public Resp findByUserId(Long userId) {
        if (userId == null) {
            return baseApiService.setResultError("用户当前未登录,无法查询订单");
        }

        List<OrderEntity> orders = orderService.findByUserId(userId);

        return baseApiService.setResultSuccess(orders);
    }
}
